#include "door_control.h"
#include "main.h"


door_control::door_control(QObject *parent) :
    QObject(parent)
{

     timer=new QTimer();
     QObject:: connect( timer, SIGNAL(timeout()), SLOT(timeDone()) );
     timer->start(1000);
     //if( whole_machine_parameter.door_near_switch)
     {
    door_near_timer=new QTimer();
    QObject:: connect( door_near_timer, SIGNAL(timeout()), SLOT(door_near_timeDone()) );
    door_near_timer->setSingleShot(true);
     }
    testtimer=new QTimer();
     QObject:: connect( testtimer, SIGNAL(timeout()), SLOT(test_timeout()) );
    // testtimer->start(30000);
     repair_mode=false;
     front_door=new door(1);
     if(whole_machine_parameter.machine_type_id%2 )back_door=new door(0);
     front_door_warn=new warn;
     back_door_warn=new warn;
}

door_control::~door_control()
{
    delete timer;
}

void door_control::timeDone()
{
    if(back_door==NULL)
    {
      if(whole_machine_parameter.machine_type_id%2 )back_door=new door(0);
      else delete back_door;
    }

    if(door_detection_failure) door_closed_OK=1;
    else
   {
    if(whole_machine_parameter.machine_type_id==circular_single_door) //("圆炉单门")
       {
          if(front_door_closed_in_place) door_closed_OK=1;
          else door_closed_OK=0;
        }
    else if(whole_machine_parameter.machine_type_id==circular_double_door)
    {
       if(front_door_closed_in_place && back_door_closed_in_place) door_closed_OK=1;
       else door_closed_OK=0;
     }
      else
    {
         if(whole_machine_parameter.machine_type_id %2)
         {
         if(front_door_closed_in_place && back_door_closed_in_place &&
               front_door_Sealing_strip_pressure_in_place && back_door_Sealing_strip_pressure_in_place )
              door_closed_OK=1;
         else door_closed_OK=0;

         }
         else
         {
              if(front_door_closed_in_place && front_door_Sealing_strip_pressure_in_place )door_closed_OK=1;
              else door_closed_OK=0;
         }
     }


        if(sterilizing && !door_closed_OK)
           {
         STOP_CMD
         breakdown=DOOR_ERROR;
         pwm->pwm_tweet(5000);
         if(!front_door_closed_in_place || !front_door_Sealing_strip_pressure_in_place)
               WARN->show_sound("前门故障！灭菌停止");
         else WARN->show_sound("后门故障！灭菌停止");
           }
    }

    if( whole_machine_parameter.door_near_switch &&
            whole_machine_parameter.machine_type_id >4)
    {
     if( ((front_door_opened_in_place &&!front_door_closed_in_place) ||
          (back_door_opened_in_place && !back_door_closed_in_place)) &&
             !door_near_timer->isActive() && !front_door_warn->isVisible() &&
             !back_door_warn->isVisible()   )
             door_near_timer->start(5000);
     if((front_door_closed_in_place && !front_door_opened_in_place && !front_door_warn->isVisible())||
             (back_door_closed_in_place && !back_door_opened_in_place && !back_door_warn->isVisible()))
         door_near_timer->start(500);

     if( (front_door_closed_in_place == front_door_opened_in_place ) && front_door_warn->isVisible() )
         front_door_warn->on_pushButton_clicked();
     if( (back_door_closed_in_place == back_door_opened_in_place ) && back_door_warn->isVisible() )
         back_door_warn->on_pushButton_clicked();
    }
}

void door_control::door_near_timeDone()
{

    if(front_door_closed_in_place && !front_door_opened_in_place && !front_door_warn->isVisible())
        front_door_warn->show_sound("前门开关故障！");
    if(back_door_closed_in_place && !back_door_opened_in_place && !back_door_warn->isVisible())
        back_door_warn->show_sound("后门开关故障！");
    if(front_door_opened_in_place && !front_door_closed_in_place ) front_door_warn->show_sound("前门未关好，请把前门关好！");
    if(back_door_opened_in_place && !back_door_closed_in_place ) back_door_warn->show_sound("后门未关好，请把后门关好！");

}


void door_control::cmd_open_front_door()
{
    front_door->open();
}

void door_control::cmd_open_back_door()
{
    back_door->open();
}

void door_control::cmd_close_front_door()
{
    front_door->close();
}

/**********************/
void door_control::cmd_close_back_door()
{
    back_door->close();
}

void door_control::cmd_halt_door()
{

    front_door->stop_close_door() ;
    front_door->stop_open_door() ;
    back_door->stop_close_door() ;
    back_door->stop_open_door() ;
    Vacuum_pump_OFF
}

void door_control::test_timeout()
{
 /*   static bool b;
    if(b) {open_front_door b=false;}
    else {close_front_door b=true;}
*/
}


bool door_control::rise_edge_true(bool input) //0->1 =1
{
    static bool a,b,c;
    return pulse_true(a,b,c,input);
}

bool door_control::down_edge_true(bool input) //1->0 =1
{
    static bool a,b,c;
    return pulse_true(a,b,c,input);
}

bool door_control::pulse_true(bool &a,bool &b,bool &c,bool input)
{
    a=input;
    if(a^b) {c=a; b=!b;}
    else c=false;
    return c;
}
